package com.hongyu.manager.media.manager.controller.app;

import cn.hutool.log.Log;
import com.hongyu.manager.media.manager.controller.base.BaseController;
import com.hongyu.manager.media.manager.model.bean.Video;
import com.hongyu.manager.media.manager.model.bean.VideoType;
import com.hongyu.manager.media.manager.model.bean.Web;
import com.hongyu.manager.media.manager.model.bean.WebSite;
import com.hongyu.manager.media.manager.model.http.RequestLog;
import com.hongyu.manager.media.manager.model.result.Result;
import com.hongyu.manager.media.manager.model.result.ResultBuilder;
import com.hongyu.manager.media.manager.service.VideoService;
import com.hongyu.manager.media.manager.service.VideoTypeService;
import io.lettuce.core.dynamic.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Controller
@CrossOrigin
@RequestMapping("/op/hongyu/avtt/app/video")
public class AppVideoController extends BaseController  {
    private static final Logger LOG = LoggerFactory.getLogger(AppVideoController.class);
    @Autowired
    VideoService videoService;
    @Autowired
    VideoTypeService videoTypeService;




    @PostMapping("/list")
    @ResponseBody
    @RequestLog(value = "分页列表")
    public Object list(@RequestParam(defaultValue = "")String query, @RequestParam(defaultValue = "")String webSite, @RequestParam(defaultValue = "")String typeName, @RequestParam(defaultValue = "0") Integer regionId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, HttpServletRequest request, HttpServletResponse response){
        LOG.info(typeName);
        Video video=new Video();
        video.setWebSite(webSite);
        video.setTitle(typeName);
        video.setStart((pageNum - 1) * pageSize);
        video.setPageSize(pageSize);
        List<Video> videoList=videoService.getList(video);
        Result<List<Video>> result = requestService.returnSuccess(new ResultBuilder<List<Video>>(), "登录成功，正在跳转 ...");
        result.setData(videoList);
        return result;
    }

    @PostMapping("/website/list")
    @ResponseBody
    @RequestLog(value = "网站分页列表")
    public Object webList(){
        List<WebSite> list=videoService.webSiteList();
        Result<List<WebSite>> result = requestService.returnSuccess(new ResultBuilder<List<WebSite>>(), "登录成功，正在跳转 ...");
        result.setData(list);
        return result;
    }

    @PostMapping("/play/times/update")
    @ResponseBody
    @RequestLog(value = "更新播放次数")
    public Object updatePalyTimes(@RequestParam(defaultValue = "")String website,@RequestParam(defaultValue = "0")Integer id){
        videoService.updatePalyTimes(id,website);

        return requestService.returnSuccess("");
    }

    @PostMapping("type/list")
    @ResponseBody
    @RequestLog(value = "更新播放次数")
    public Object typeList(@RequestParam(defaultValue = "")String webSite){
        VideoType video=new VideoType();
        video.setWebSite(webSite);

        List<VideoType> list=videoTypeService.searchTypeList(video);
        List<Video> videos=new ArrayList<>();
        for (VideoType type:list){
            Video vid=new Video();
            LOG.info(type.getTypeName());
            vid.setWebSite(type.getWebSite());
            vid.setTitle(type.getTypeName());
            videos.add(vid);

        }
        Result<List<Video>> result = requestService.returnSuccess(new ResultBuilder<List<Video>>(), "登录成功，正在跳转 ...");
        result.setData(videos);
        return result;
    }

    @PostMapping("web/list")
    @ResponseBody
    @RequestLog(value = "更新播放次数")
    public Object webList(@RequestParam(defaultValue = "")String query, @RequestParam(defaultValue = "")String webSite, @RequestParam(defaultValue = "")String typeName, @RequestParam(defaultValue = "0") Integer regionId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, HttpServletRequest request, HttpServletResponse response){

        Web web=new Web();
        web.setStart((pageNum - 1) * pageSize);
        web.setPageSize(pageSize);
        List<Web> list=videoService.webList(web);
        Result<List<Web>> result = requestService.returnSuccess(new ResultBuilder<List<Web>>(), "登录成功，正在跳转 ...");
        result.setData(list);
        return result;
    }
    @RequestMapping("getMedia")
    protected void getMedia(@PathVariable("fileName") String fileName, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取要传输的MP4文件路径
        String filePath = "G://abc.mp4";
        //通过文件流传输
        try (InputStream inputStream = new FileInputStream(new File(filePath));
             OutputStream outputStream = response.getOutputStream()) {
            // 设置及获取响应头信息
            response.setContentType("video/mp4");
            response.setHeader("Accept-Ranges", "bytes");
            String rangeString = request.getHeader("Range");//下载时会为null
            response.setHeader("Content-Length", String.valueOf(inputStream.available()));
            //区别下载和不下载的情况，不下载时设置Content-Range
            if (rangeString!=null){
                long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
                response.setHeader("Content-Range", String.valueOf(range + (inputStream.available()-1)));//拖动进度条时的断点
            }
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}
